package com.qlslylq.ad.sdk.config;

import android.content.Context;
import android.text.TextUtils;

import com.qlslylq.ad.sdk.application.AdMergeSDK;
import com.qlslylq.ad.sdk.enums.AdPlatformEnum;
import com.qlslylq.ad.sdk.enums.ErrorEnum;
import com.qlslylq.ad.sdk.interfaces.InitCallback;
import com.qlslylq.ad.sdk.util.GsonUtils;
import com.qlslylq.ad.sdk.util.Log;
import com.qq.e.comm.managers.GDTAdSdk;
import com.qq.e.comm.managers.IGDTAdManager;

import static com.qlslylq.ad.sdk.model.rsp.MediaWebVO.NOT_CONFIG;


/**
 * %s初始化控制类<br/>
 */
public class YlhMgrHolder {

    private static boolean initSuccess = false;

    public static IGDTAdManager get() {
        return GDTAdSdk.getGDTAdManger();
    }

    public static void init(final Context context, final InitCallback callback) {
        doInit(context, callback);
    }

    /**
     * 初始化<br/>
     */
    private static void doInit(Context context, final InitCallback callback) {
        AdPlatformEnum platform = AdPlatformEnum.PLATFORM_YLH;
        if (!initSuccess) {
            try {
                String appId = AdMergeSDK.getMediaWebVO().getAppIdByPlatform(platform);
                if (NOT_CONFIG.equals(appId)) {
                    Log.d(String.format("未配置%s账号到蒂烨平台", platform.getId()));
                    return;
                }
                if (TextUtils.isEmpty(appId)) {
                    Log.d(String.format("%s初始化失败：" + GsonUtils.gsonString(ErrorEnum.ERR_INIT_INVALID_ARG.rsp(platform, null)), platform.getId()));
                    if (callback != null) {
                        callback.fail(platform, ErrorEnum.ERR_INIT_INVALID_ARG.getId(), ErrorEnum.ERR_INIT_INVALID_ARG.getName());
                    }
                    return;
                }

                GDTAdSdk.init(context, appId);
                initSuccess = true;
                Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败"), platform.getId()));
            } catch (Exception e) {
                e.printStackTrace();
                Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败") + "：" + e.getMessage(), platform.getId()));
                if (callback != null) {
                    callback.fail(platform, ErrorEnum.ERR_INIT_THIRD_SDK.getId(), e.getMessage());
                }
            }

        }
    }

    public static boolean isInitSuccess() {
        return initSuccess;
    }

    /**
     * 设置是否启用平台<br/>
     */
    public static void enablePlatform(boolean enable) {
        initSuccess = enable;
    }
}
